import request from '@/request';
import { ElMessage } from 'element-plus';

/* 获取商品列表 */
export const getGoodsListHook = params => {
    return new Promise((resolve, reject) => {
        request('queryGoodsListServ', params).then(res => {
            if (res.code === 0) {
                resolve(res);
            } else {
                ElMessage.error(res.msg);
                reject();
            }
        });
    });
};
/* 获取一级类目列表 */
export const getGoodsClassListHook = params => {
    return new Promise((resolve, reject) => {
        request('goodsClassList', params).then(({ code, data, msg }) => {
            if (code === 0) {
                let list = Array.isArray(data.list) ? data.list : [];
                data.list = list.map(item => ({ ...item, hasChildren: !item.hasChildren }));
                data.totalCount = data.totalCount || 0;
                resolve(data);
            } else {
                ElMessage.error(msg);
                reject();
            }
        });
    });
};

/* 获取子级类目列表 */
export const getGoodsClassChildListHook = id => {
    return new Promise((resolve, reject) => {
        const disabled = [{ hasChildren: true, disabled: true }];
        request('goodsClassChildList', { classIds: [id] })
            .then(({ code, data, msg }) => {
                if (code === 0) {
                    const newData = data || {};
                    let list = Array.isArray(newData[id]) ? newData[id] : [];
                    // 兼容接口请求错误，再次点击可重新请求
                    if (list.length === 0) list = disabled;
                    const newList = list.map(item => ({
                        ...item,
                        hasChildren: !item.hasChildren
                    }));
                    resolve(newList);
                } else {
                    ElMessage.error(msg);
                    resolve(disabled);
                }
            })
            .catch(err => {
                resolve(disabled);
            });
    });
};

/* 获取一级分组列表 */
export const getGoodsCategoryListHook = params => {
    return new Promise((resolve, reject) => {
        request('queryCategoryListServ', params).then(({ code, data, msg }) => {
            if (code === 0) {
                let list = Array.isArray(data.list) ? data.list : [];
                data.list = list.map(item => ({ ...item, hasChildren: !item.hasChildren }));
                data.totalCount = data.totalCount || 0;
                resolve(data);
            } else {
                ElMessage.error(msg);
                reject();
            }
        });
    });
};

/* 获取子分组列表 */
export const getGoodsCategoryChildListHook = id => {
    return new Promise((resolve, reject) => {
        const disabled = [{ hasChildren: true, disabled: true }];
        request('categoryChildList', { categoryIds: [id] })
            .then(({ code, data, msg }) => {
                if (code === 0) {
                    const newData = data || {};
                    let list = Array.isArray(newData[id]) ? newData[id] : [];
                    // 兼容接口请求错误，再次点击可重新请求
                    if (list.length === 0) list = disabled;
                    const newList = list.map(item => ({
                        ...item,
                        hasChildren: !item.hasChildren
                    }));
                    resolve(newList);
                } else {
                    ElMessage.error(msg);
                    resolve(disabled);
                }
            })
            .catch(err => {
                resolve(disabled);
            });
    });
};
